//
//  WmcPlaceholderTextView.swift
//  yp_cloud
//
//  Created by Json on 2022/6/10.
//

import UIKit
import SnapKit

class WmcPlaceholderTextView: WmcLimitedTextView {

    private(set) lazy var placeholderLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textColor = UIColor.wmc_grayH8_8A8A99
        return label
    }()
    
    override var text: String! {
        didSet {
            setPlaceholderLabel(isHidden: self.hasText)
        }
    }
    
    override var font: UIFont? {
        didSet {
            placeholderLabel.font = font
        }
    }
    /// 占位文字
    var placeholder: String? {
        set {
            placeholderLabel.text = newValue
        }
        get {
            placeholderLabel.text
        }
    }
    
    override var textContainerInset: UIEdgeInsets {
        didSet {
            if placeholderLabel.superview == nil { return }
            placeholderLabel.snp.remakeConstraints { make in
                makePlaceholderLabelConstraints(make: make)
            }
        }
    }
    
    // MARK: Init
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setup()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 初始化配置
    private func setup() {
        textColor = UIColor.wmc_grayH2_323233
        tintColor = UIColor.wmc_mainTheme
        // placeholderLabel
        setPlaceholderLabel(isHidden: false)
        // 监听文字输入
        NotificationCenter.default.rx.notification(UITextView.textDidChangeNotification, object: self).subscribe(onNext: {
            [weak self] noti in
            guard let self = self else { return }
            guard let tv = noti.object as? UITextView,
                  self == tv else { return }
            self.setPlaceholderLabel(isHidden: self.hasText)
        }).disposed(by: rx.disposeBag)
    }
    
    // MARK: Private method
    // 添加/删除placeholderLabel
    private func setPlaceholderLabel(isHidden: Bool) {
        // 注:对placeholderLabel进行添加/删除而不是显示/隐藏,是因为显示/隐藏placeholderLabel,会出现文字内容超出TextView需要滚动的时候无法滚动问题
        if isHidden {
            placeholderLabel.removeFromSuperview()
        } else {
            if placeholderLabel.superview != nil { return }
            addSubview(placeholderLabel)
            placeholderLabel.snp.makeConstraints { make in
                makePlaceholderLabelConstraints(make: make)
            }
        }
        
    }
    
    private func makePlaceholderLabelConstraints(make: ConstraintMaker) {
        let top = textContainerInset.top
        let left = textContainer.lineFragmentPadding + textContainerInset.left
        let right = textContainer.lineFragmentPadding + textContainerInset.right
        let bottom = textContainerInset.bottom
        let widthOffset = left + right
        let heightOffset = top + bottom
        make.top.equalToSuperview().offset(top)
        make.left.equalToSuperview().offset(left)
        make.width.equalToSuperview().offset(-widthOffset)
        make.height.lessThanOrEqualToSuperview().offset(-heightOffset)
    }
    
    
}
